package train_array.day1;

import java.util.HashSet;
import java.util.Scanner;

public class Main {


    static long sum = 0;

    public static void cal(int[] arr,int n){
        HashSet<Integer> set = new HashSet<>();
        int num = 0;
        boolean flag=false;
        for(int i=n;i<arr.length;i++){
            set.add(arr[i]);
            int m = fun(set);
            sum += m;
            if(m==3){
                   flag=true;
                   num = i;
                   break;
                }
        }

        if(flag){
            sum += 3L *(arr.length-num-1);
        }
    }

    public static int fun(HashSet<Integer> set){
        if(!set.contains(0)) return 0;
        if(!set.contains(1)) return 1;
        if(!set.contains(2)) return 2;
        return 3;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int len = Integer.parseInt(sc.nextLine());

        String[] s = sc.nextLine().split(" ");
        int[] arr = new int[len];
        for(int i=0;i<len;i++){
            arr[i] = Integer.parseInt(s[i]);
        }

        for(int i=0;i<len;i++){
            cal(arr,i);
        }
        System.out.println(sum);

    }
}
